package com.forlgb.powercloud.authorization.service.impl;

import com.forlgb.powercloud.authorization.config.OkHttpCli;
import com.forlgb.powercloud.authorization.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;

@Service
public class AuthServiceImpl implements AuthService {

    @Value("${security.oauth2.client.client-id}")
    private String clientId;

    @Value("${security.oauth2.client.client-secret}")
    private String clientSecret;

    @Value("${security.oauth2.client.access-token-uri}")
    private String tokenUri;

    @Autowired
    private OkHttpCli okHttpCli;

    @Override
    public String login(String username, String password) {
        Assert.hasText(username, "用户名不能为空!");
        Assert.hasText(password, "密码不能为空!");
        HashMap map = new HashMap();
        String[] data = {clientId, clientSecret};
        map.put("username", username);
        map.put("password", password);
        map.put("grant_type", "password");
        String msg = okHttpCli.doBasePost(tokenUri, map, data);
        return msg;
    }
}
